/**
 * 用于展示OD线集合
 * @param {Earth} options.earth 渲染容器
 * @param {Array.<object>} options.data<object>.startPos 起始点 [经度，纬度，高度] 
 * @param {Array.<object>} options.data<object>.endPos 终止点 [经度，纬度，高度] 
 * @param {Array.<object>} options.data<object>.heightRatio 曲率控制，默认1.0，越大曲线抬得越高
 * @param {Array.<object>} options.data<object>.width 宽度
 * @param {Array.<object>} options.data<object>.color 颜色 colorArray
 * @param {Array.<object>} options.data<object>.bgColor 背景颜色 colorArray
 * @param {Array.<object>} options.data<object>.bidirectional 运行形式：0 正向运动 1 反向运动 2 双向运动
 * @param {Array.<object>} options.data<object>.startTime OD线从某个时刻开始播放
 * @param {Array.<object>} options.data<object>.duration OD线从头到尾播放的总时长
 * @param {Array.<object>} options.data<object>.pointNum 单条线上生成点的数量
 * 
 * @methods update(options) 更新多边形数据
 * @methods onclick() DrawPolygon点击事件 this.update更新数据  this.polygon 为 XE.Obj.Polygon 原始对象
 * 
 * @returns {Polygon} Polygon实例
 */

function DrawOdline (options) {
    let radian = Math.PI / 180;
    let ODlines = new XE.Obj.ODLines(options.earth)
    let lineData = []
    options.data.forEach(lineOptions => {
        let positions = getBSRPoints(
            lineOptions.startPos[0],
            lineOptions.startPos[1],
            lineOptions.endPos[0],
            lineOptions.endPos[1],
            lineOptions.heightRatio || 100000,
            lineOptions.pointNum || 50
        )
        lineData.push({
            positions: positions.map(item => {
                return [
                    item[0] * radian,
                    item[1] * radian,
                    item[2]
                ]
            }),
            width: lineOptions.width || 3.0,
            color: lineOptions.color || [1, 1, 1, 0.8],
            bgColor: lineOptions.bgColor || [0, 0, 0, 0.3],
            bidirectional: lineOptions.bidirectional || 0,
            startTime: lineOptions.startTime || 0,
            duration: lineOptions.duration || 3.0,
        })
    })
    ODlines.data = lineData
    ODlines.playing = true
    
    ODlines._node = this
    this.XEObj = ODlines
    this.data = options.data || null
    this.changeStatus = options.changeStatus === undefined ? true : options.changeStatus
}


DrawOdline.prototype.destroy = function () {
    this.XEObj._node = null
    this.XEObj.destroy()
}

DrawOdline.prototype.setShow = function (options) {
    this.XEObj.show = options
}

// DrawOdline.prototype.click = function () {
//     if (this.onclick) { this.onclick() }
// }




// 贝塞尔曲线二维转三维  返回一个三维点数组
// 参数： x1,y1,x2,y2,h 两点经纬度坐标和飞线高度  每条曲线包含num个坐标
function getBSRPoints(x1, y1, x2, y2, h, pointNum) {
    let point1 = [y1, 0]
    let point2 = [(y2 + y1) / 2, h]
    let point3 = [y2, 0]
    let num = pointNum || 50
    let arr = getBSR(point1, point2, point3, num)
    let arr3d = []
    for (let i in arr) {
        let x = (x2 - x1) * (arr[i][0] - y1) / (y2 - y1) + x1
        arr3d.push([x, arr[i][0], arr[i][1]])
    }
    arr3d.push([
        x2, y2
    ])
    return arr3d
}
// 生成贝塞尔曲线
function getBSR(point1, point2, point3, num) {
    var ps = [{ x: point1[0], y: point1[1] }, { x: point2[0], y: point2[1] }, { x: point3[0], y: point3[1] }]
    let guijipoints = CreateBezierPoints(ps, num);
    return guijipoints
}
// 贝赛尔曲线算法
// 参数：
// anchorpoints: [{ x: 116.30, y: 39.60 }, { x: 37.50, y: 40.25 }, { x: 39.51, y: 36.25 }]
function CreateBezierPoints(anchorpoints, pointsAmount) {
    var points = [];
    for (var i = 0; i < pointsAmount; i++) {
        var point = MultiPointBezier(anchorpoints, i / pointsAmount)
        points.push([point.x, point.y]);
    }
    return points;
}
function MultiPointBezier(points, t) {
    var len = points.length;
    var x = 0, y = 0;
    var erxiangshi = function (start, end) {
        var cs = 1, bcs = 1;
        while (end > 0) {
            cs *= start;
            bcs *= end;
            start--;
            end--;
        }
        return (cs / bcs);
    };
    for (var i = 0; i < len; i++) {
        var point = points[i];
        x += point.x * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (erxiangshi(len - 1, i));
        y += point.y * Math.pow((1 - t), (len - 1 - i)) * Math.pow(t, i) * (erxiangshi(len - 1, i));
    }
    return { x: x, y: y };
}





export default DrawOdline